#Pydantic 数据校验
from datetime import datetime
from pydantic import BaseModel
from typing import Optional, List

class UserCreate(BaseModel):
    username: str
    password: str
    role: str = "student"

class UserOut(BaseModel):
    id: int
    username: str
    role: str

    class Config:
        from_attributes = True

class CourseCreate(BaseModel):
    name: str
    teacher_id: int
    class Config:
        from_attributes = True

class CourseOut(BaseModel):
    id: int
    name: str

    class Config:
        orm_mode = True

class UserLogin(BaseModel):
    username: str
    password: str

# 登录成功返回的 Token
class Token(BaseModel):
    access_token: str
    token_type: str = "bearer"

class ProblemCreate(BaseModel):
    title: str
    description: str
    type: str
    answer: Optional[str] = None
    test_cases: Optional[str] = None

class ProblemOut(BaseModel):
    id: int
    title: str
    description: str
    type: str
    answer: Optional[str]
    test_cases: Optional[str]

    class Config:
        from_attributes = True

class HomeworkCreate(BaseModel):
    title: str
    description: Optional[str] = None
    problems: List[int]   # 前端传题目ID数组
    deadline: Optional[datetime] = None
    teacher_id: int

class HomeworkOut(BaseModel):
    id: int
    title: str
    description: Optional[str]
    problems: List[int]
    deadline: Optional[datetime]
    teacher_id: int

    class Config:
        from_attributes = True

class SubmissionCreate(BaseModel):
    homework_id: int
    problem_id: int
    code: str

class SubmissionOut(BaseModel):
    id: int
    homework_id: int
    problem_id: int
    student_id: int
    code: str
    status: str
    submit_time: datetime

    class Config:
        from_attributes = True